import { Component } from "@angular/core";
import { AdMobFree } from "@ionic-native/admob-free/ngx";
import { Clipboard } from "@ionic-native/clipboard/ngx";
import { NdefRecord, NFC } from "@ionic-native/nfc/ngx";
import { ActionSheetController, AlertController, LoadingController, ModalController, ToastController, ViewDidLeave } from "@ionic/angular";
import { LocalStorage } from "../local-storage.service";
import { Global } from "./../global.service";

@Component({
	selector: "app-tag-book",
	templateUrl: "./tag-book.page.html",
	styleUrls: ["./tag-book.page.scss"],
})
export class TagBookPage implements ViewDidLeave {

	TNF = {
		0: "Empty Record",
		1: "Well-Known Record",
		2: "MIME Media Record",
		3: "Absolute URI Record",
		4: "External Record",
		5: "Unknown Record",
		6: "Unchanged Record",
	};

	tags: NdefRecord[];

	type = "";

	currentUseTag: NdefRecord;

	currentRewardInfo: any;

	id: string;

	constructor(
		private alertCtrl: AlertController,
		private loadingCtrl: LoadingController,
		private modalCtrl: ModalController,
		private toastCtrl: ToastController,
		private actionSheetCtrl: ActionSheetController,
		private localStorage: LocalStorage,
		private clipboard: Clipboard,
		private adMobFree: AdMobFree,
		private global: Global,
		private nfc: NFC
	) {
		this.currentUseTag = undefined;
		this.tags = this.localStorage.getData(Global.TAGS);
		if (!this.tags) {
			this.tags = [];
		}
		this.adMobFree.on(this.adMobFree.events.REWARD_VIDEO_REWARD).subscribe(event => {
			this.currentRewardInfo = event;
			console.log(this.currentRewardInfo);
		});
		this.adMobFree.on(this.adMobFree.events.REWARD_VIDEO_CLOSE).subscribe(async () => {
			if (this.currentRewardInfo.rewardType) {
				switch (this.currentRewardInfo.rewardType) {
					case "share":
						// this.share();
						break;
					case "write":
						this.write();
						break;
				}
			}
		});
	}

	ionViewDidLeave(): void {

	}

	raw(tag: NdefRecord) {
		return JSON.stringify(tag);
	}

	private async write() {
		console.log(this.currentUseTag);
		await this.nfc.write([this.currentUseTag]);
	}

	parse(data: number[]): string {
		let result = "";
		for (const i of data) {
			result += String.fromCharCode(i);
		}
		return result;
	}

	private async prepareInsertAd() {
		this.adMobFree.interstitial.config({
			id: Global.INSERT_ID,
			isTesting: false,
			autoShow: true
		});
		await this.adMobFree.rewardVideo.prepare();
	}

	private async prepareVideoAd(id: string) {
		const loading = await this.loadingCtrl.create();
		await loading.present();
		this.currentRewardInfo = {};
		this.adMobFree.rewardVideo.config({
			id,
			isTesting: false,
			autoShow: true
		});
		this.adMobFree.rewardVideo.prepare().then().finally(() => {
			loading.dismiss();
		});
	}

	async menu(tag: NdefRecord) {
		this.currentUseTag = tag;
		this.currentRewardInfo = {};
		const actionSheet = await this.actionSheetCtrl.create({
			buttons: [{
				text: this.translate("WRITE_INTO_NFC_DEVICE"),
				handler: async () => {
					if (this.global.needShowAd && !this.global.isFree) {
						const alert = await this.alertCtrl.create({
							header: this.translate("GET_VIP"),
							message: this.translate("GET_VIP_MESSAGE"),
							buttons: [{
								text: this.translate("WATCH_AND_UNLOCK"),
								handler: () => {
									this.prepareVideoAd(Global.VIDEO_WRITE_ID);
								}
							}, {
								text: this.translate("CANCEL"),
								role: "cancel",
							}]
						});
						alert.present();
					} else {
						this.write();
					}
				}
			}, {
				text: this.translate("COPY_TO_CLIPBOARD"),
				handler: () => {
					// if (this.global.needShowAd) {
					// 	this.prepareInsertAd();
					// }
					this.clipboard.copy(JSON.stringify(tag)).then(() => {
						this.toast(this.translate("COPY_TO_CLIPBOARD_SUCCESS"));
					});
				}
			}, {
				text: this.translate("CANCEL"),
				role: "cancel"
			}]
		});
		await actionSheet.present();
	}

	private async toast(message: string) {
		const toast = await this.toastCtrl.create({
			message,
			duration: 3000
		});
		await toast.present();
	}

	async dismiss() {
		await this.modalCtrl.dismiss();
	}


	translate(key: string) {
		return this.global.translate(key);
	}
}
